package logic;

import entity.DALException;
import entity.IOperatorDAO;
import entity.OperatorDTO;

public class Logic implements ILogic {
	
	private int userId = 0;
	private IOperatorDAO data;

	public Logic(IOperatorDAO data) {
		this.data = data;
	}

	@Override
	public void login(String userId, String password) {
		if(!userId.isEmpty() && !password.isEmpty()){
			int id = Integer.parseInt(userId);
			
			// superadmin
			if(id == 10 && password.equals("02324it!")){
				this.userId = id;
				return;
			}
			
			try {
				OperatorDTO user = data.getOperator(id);
				if(user != null){
					if(user.getPassword().equals(password)){
						this.userId = id;
						return;
					}
				}
			} catch (DALException e) {}
		}
		return;
	}

	@Override
	public boolean isLoggedIn() {
		return userId != 0;
	}

	@Override
	public boolean isAdmin() {
		return userId == 10;
	}
	
	@Override
	public int getUserID(){
		return userId;
	}
	
	@Override
	public void changePassword(String[] passwords) throws  DALException{
		data.changePassword(userId, passwords);
		return;
	}
	
	@Override
	public double calculateNetWeight(double gross,  double tare){
		return gross-tare;
	}
	
}
